/*
 * Copyright 2022-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.aurora.generator.autoconfiguration;

import org.aurora.generator.core.impl.CachedUidGenerator;
import org.aurora.generator.core.impl.DefaultUidGenerator;
import org.aurora.generator.core.worker.DisposableWorkerIdAssigner;
import org.aurora.generator.core.worker.repository.WorkerNodeRepository;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.*;

/**
 * 雪花ID配置器
 *
 * @author Evan Zhai
 * @since 1.0.0
 */
@Configuration
@ComponentScan(basePackageClasses = {WorkerNodeRepository.class})
@EnableConfigurationProperties(value = DefaultCachedUidGeneratorProperties.class)
public class UidGeneratorAutoConfiguration {

	private final DefaultCachedUidGeneratorProperties defaultCachedUidGeneratorProperties;

	public UidGeneratorAutoConfiguration(DefaultCachedUidGeneratorProperties defaultCachedUidGeneratorProperties) {
		this.defaultCachedUidGeneratorProperties = defaultCachedUidGeneratorProperties;
	}

	@Bean
	@Primary
	public DefaultUidGenerator defaultUidGenerator() {
		DefaultUidGenerator defaultUidGenerator = new DefaultUidGenerator();
		defaultUidGenerator.setTimeBits(defaultCachedUidGeneratorProperties.getTimeBits());
		defaultUidGenerator.setWorkerBits(defaultCachedUidGeneratorProperties.getWorkerBits());
		defaultUidGenerator.setSeqBits(defaultCachedUidGeneratorProperties.getSeqBits());
		defaultUidGenerator.setEpochStr(defaultCachedUidGeneratorProperties.getEpochStr());
		defaultUidGenerator.setWorkerIdAssigner(this.defaultWorkerIdAssigner());
		return defaultUidGenerator;
	}

	@Lazy
	@Bean
	public CachedUidGenerator cachedUidGenerator() {
		CachedUidGenerator cachedUidGenerator = new CachedUidGenerator();
		cachedUidGenerator.setBoostPower(defaultCachedUidGeneratorProperties.getBoostPower());
		cachedUidGenerator.setScheduleInterval(defaultCachedUidGeneratorProperties.getScheduleInterval());
		cachedUidGenerator.setPaddingFactor(defaultCachedUidGeneratorProperties.getPaddingFactor());
		cachedUidGenerator.setTimeBits(defaultCachedUidGeneratorProperties.getTimeBits());
		cachedUidGenerator.setWorkerBits(defaultCachedUidGeneratorProperties.getWorkerBits());
		cachedUidGenerator.setSeqBits(defaultCachedUidGeneratorProperties.getSeqBits());
		cachedUidGenerator.setEpochStr(defaultCachedUidGeneratorProperties.getEpochStr());
		cachedUidGenerator.setWorkerIdAssigner(this.defaultWorkerIdAssigner());
		return cachedUidGenerator;
	}

	@Bean
	@ConditionalOnMissingBean(value = DisposableWorkerIdAssigner.class)
	public DisposableWorkerIdAssigner defaultWorkerIdAssigner() {
		return new DisposableWorkerIdAssigner();
	}

}
